﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VietThuongLMS.ViewModels;
using VietThuongLMS.Utils;

///Author: NHDuc
///Created date: 22/11/2013
namespace VietThuongLMS.Services
{
    public class AlbumService
    {
        public List<Album> GetAlbumList()
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    List<Album> list = context.Albums.ToList();
                    return list;
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AlbumService::GetAlbumList", ex.StackTrace, ex.Message);
                return null;
            }
        }

        public static AlbumViewModel GetAlbumList(int pageNum, int pageIndex)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                //hinh anh
                List<Album> list = context.Albums.Where(m => m.Type == 1).OrderByDescending(m => m.ID).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                int total = context.Albums.Count();
                //video
                List<Album> videoList = context.Albums.Where(m => m.Type == 2).OrderByDescending(m => m.ID).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                int videoTotal = context.Albums.Count();

                return new AlbumViewModel(list, total, videoList, videoTotal);
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountService::GetAlbumList", ex.StackTrace, ex.Message);
                return new AlbumViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }

        public static AlbumViewModel GetAlbumAllList(int pageNum, int pageIndex)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                List<Album> list = context.Albums.Where(w => w.Type != Constant.GENERAL_STATUS_DELETED).OrderByDescending(m => m.ID).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                int total = context.Albums.Where(w => w.Type != Constant.GENERAL_STATUS_DELETED).Count();

                return new AlbumViewModel(list, total);
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountService::GetAlbumAllList", ex.StackTrace, ex.Message);
                return new AlbumViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }

        public static AlbumViewModel GetAlbumSepList(int pageNum, int pageIndex, int VpageNum, int VpageIndex)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                //hinh anh
                List<Album> list = context.Albums.Where(m => m.Type == 1).OrderByDescending(m => m.ID).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                int total = context.Albums.Count();
                //video
                List<Album> videoList = context.Albums.Where(m => m.Type == 2).OrderByDescending(m => m.ID).Skip(VpageNum * (VpageIndex - 1)).Take(VpageNum).ToList();
                int videoTotal = context.Albums.Count();

                return new AlbumViewModel(list, total, videoList, videoTotal);
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountService::GetAlbumList", ex.StackTrace, ex.Message);
                return new AlbumViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }

        public static AlbumViewModel GetAlbumByTagList(string tag, int pageNum, int pageIndex)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                //hinh anh
                List<Album> list = context.Albums.Where(m => m.Type == 1 && m.Tag == tag).OrderByDescending(m => m.ID).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                int total = context.Albums.Count();
                //video
                List<Album> videoList = context.Albums.Where(m => m.Type == 2 && m.Tag == tag).OrderByDescending(m => m.ID).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                int videoTotal = context.Albums.Count();

                return new AlbumViewModel(list, total, videoList, videoTotal);
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountService::GetAlbumByTagList", ex.StackTrace, ex.Message);
                return new AlbumViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool InsertAlbum(Album comp)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                context.AddToAlbums(comp);
                context.SaveChanges();
                return true;

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AlbumServices::InsertAlbum", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool Edit(Album newModel)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                Album model = context.Albums.Where(c => c.ID == newModel.ID).FirstOrDefault();
                if (model != null)
                {
                    model.TenAlbum = newModel.TenAlbum;
                    model.Tag = newModel.Tag;
                    model.Type = newModel.Type;
                    model.Url = newModel.Url;
                    context.SaveChanges();
                    return true;
                }
                return false;

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AlbumServices::Edit", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static Album GetAlbumById(int id)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                return context.Albums.Where(c => c.ID == id).FirstOrDefault();
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AlbumServices::GetAlbumById", ex.StackTrace, ex.Message);
                return null;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool DeleteAlbum(int id)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                Album obj = context.Albums.Where(c => c.ID == id).FirstOrDefault();
                if (obj != null)
                {
                    obj.Type = Utils.Constant.GENERAL_STATUS_DELETED;
                    context.SaveChanges();
                    return true;
                }
                return false;

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AlbumServices::DeleteAlbum", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }



    }
}